package com.navi168.fwq21.monitoring;

import java.util.Calendar;

import com.navi168.fwq21.R;

import android.R.id;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

public class TrackIndex extends Activity {
	private Button okButton;
	private Button canceButton;
	private EditText begind;
	private EditText begint;
	private EditText endd;
	private EditText endt;

	private int year;
	private int month;
	private int day;
	private int hour;
	private int minute;
	private final int DATE_DIALOG1 = 1;

	private final int TIME_DIALOG1 = 2;

	private final int DATE_DIALOG2 = 3;

	private final int TIME_DIALOG2 = 4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gjhfmap);
		findviews();
		setliseters();

	}

	private void setliseters() {
		okButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(TrackIndex.this, TrackShow.class);
				startActivity(intent);

			}
		});

		canceButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				TrackIndex.this.finish();

			}
		});

		begind.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialog(1);

			}
		});

		begint.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialog(2);

			}
		});
		endd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialog(3);

			}
		});
		endt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialog(4);

			}
		});
	}

	private void findviews() {
		okButton = (Button) findViewById(R.id.ckbutton);
		canceButton = (Button) findViewById(R.id.cancebutton);
		begind = (EditText) findViewById(R.id.begin_d);
		begint = (EditText) findViewById(R.id.begin_t);
		endd = (EditText) findViewById(R.id.end_d);
		endt = (EditText) findViewById(R.id.end_t);

	}

	protected Dialog onCreateDialog(int id) {
		// 用来获取日期和时间的
		Calendar calendar = Calendar.getInstance();

		Dialog dialog = null;
		switch (id) {
		case DATE_DIALOG1:
			DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {
				@Override
				public void onDateSet(DatePicker datePicker, int year,
						int month, int dayOfMonth) {

					// Calendar月份是从0开始,所以month要加1
					begind.setText(year + "-" + (++month) + "-" + dayOfMonth);

				}
			};
			dialog = new DatePickerDialog(this, dateListener,
					calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
					calendar.get(Calendar.DAY_OF_MONTH));
			break;
		case TIME_DIALOG1:
			TimePickerDialog.OnTimeSetListener timeListener = new TimePickerDialog.OnTimeSetListener() {

				@Override
				public void onTimeSet(TimePicker timerPicker, int hourOfDay,
						int minute) {
					begint.setText(hourOfDay + ":" + minute);

				}
			};
			dialog = new TimePickerDialog(this, timeListener,
					calendar.get(Calendar.HOUR_OF_DAY),
					calendar.get(Calendar.MINUTE), false); // 是否为二十四制
			break;

		case DATE_DIALOG2:
			DatePickerDialog.OnDateSetListener dateListener2 = new DatePickerDialog.OnDateSetListener() {
				@Override
				public void onDateSet(DatePicker datePicker, int year,
						int month, int dayOfMonth) {

					// Calendar月份是从0开始,所以month要加1
					endd.setText(year + "-" + (++month) + "-" + dayOfMonth);
				}
			};
			dialog = new DatePickerDialog(this, dateListener2,
					calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
					calendar.get(Calendar.DAY_OF_MONTH));
			break;
		case TIME_DIALOG2:
			TimePickerDialog.OnTimeSetListener timeListener2 = new TimePickerDialog.OnTimeSetListener() {

				@Override
				public void onTimeSet(TimePicker timerPicker, int hourOfDay,
						int minute) {
					endt.setText(hourOfDay + ":" + minute);
				}
			};
			dialog = new TimePickerDialog(this, timeListener2,
					calendar.get(Calendar.HOUR_OF_DAY),
					calendar.get(Calendar.MINUTE), false); // 是否为二十四制
			break;

		default:
			break;
		}
		return dialog;
	}

}
